﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BIZ;
using DAL;

namespace SinhNhat.admin
{
    public partial class EditCategory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["id"] == null)
                {
                    ShowError();
                }
                else
                {
                    Guid categoryID = new Guid(Request.Params["id"]);
                    CategoryBIZ categoryBIZ = new CategoryBIZ();
                    CATEGORY category = categoryBIZ.GetByID(categoryID);

                    if (category == null)
                    {
                        ShowError();
                    }

                    hdID.Value = categoryID.ToString();
                    txtNew.Text = category.CategoryName;
                    txtURl.Text = category.Url;
                }
            }
        }

        private void ShowError()
        {
            Response.Write("<script type='text/javascript'>alert('CategoryID is not correct');window.location = '/admin/AdminCategory.aspx';</script>");
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            btnUpdate.Click += new EventHandler(btnUpdate_Click);
        }

        void btnUpdate_Click(object sender, EventArgs e)
        {
            CategoryBIZ categoryBIZ = new CategoryBIZ();
            CATEGORY category = new CATEGORY();

            category.CategoryID = new Guid(hdID.Value);
            category.CategoryName = txtNew.Text.Trim();
            categoryBIZ.Edit(category);

            Response.Redirect("/admin/AdminCategory.aspx");
        }
    }
}